from enum import Enum, IntEnum
from dataclasses import dataclass
from typing import List, Tuple

from ctypes import Structure, c_char, c_int, c_long, c_float, c_double

# 常量定义
ASICAMERA_ID_MAX = 256


class CameraStates(IntEnum):
    cameraIdle      = 0,
    cameraWaiting   = 1,
    cameraExposing  = 2,
    cameraReading   = 3,
    cameraDownload  = 4,
    cameraError     = 5

class SensorType(IntEnum):
    Monochrome      = 0,
    Color           = 1,
    RGGB            = 2,
    CMYG            = 3,
    CMYG2           = 4,
    LRGB            = 5

# 枚举类型迁移
class ASI_BAYER_PATTERN(IntEnum):
    ASI_BAYER_RG = 0
    ASI_BAYER_BG = 1
    ASI_BAYER_GR = 2
    ASI_BAYER_GB = 3


class ASI_IMG_TYPE(IntEnum):
    ASI_IMG_RAW8 = 0
    ASI_IMG_RGB24 = 1
    ASI_IMG_RAW16 = 2
    ASI_IMG_Y8 = 3
    ASI_IMG_END = -1


class ASI_GUIDE_DIRECTION(IntEnum):
    ASI_GUIDE_NORTH = 0
    ASI_GUIDE_SOUTH = 1
    ASI_GUIDE_EAST = 2
    ASI_GUIDE_WEST = 3


class ASI_FLIP_STATUS(IntEnum):
    ASI_FLIP_NONE = 0
    ASI_FLIP_HORIZ = 1
    ASI_FLIP_VERT = 2
    ASI_FLIP_BOTH = 3


class ASI_CAMERA_MODE(IntEnum):
    ASI_MODE_NORMAL = 0
    ASI_MODE_TRIG_SOFT_EDGE = 1
    ASI_MODE_TRIG_RISE_EDGE = 2
    ASI_MODE_TRIG_FALL_EDGE = 3
    ASI_MODE_TRIG_SOFT_LEVEL = 4
    ASI_MODE_TRIG_HIGH_LEVEL = 5
    ASI_MODE_TRIG_LOW_LEVEL = 6
    ASI_MODE_END = -1


class ASI_TRIG_OUTPUT_PIN(IntEnum):
    ASI_TRIG_OUTPUT_PINA = 0
    ASI_TRIG_OUTPUT_PINB = 1
    ASI_TRIG_OUTPUT_NONE = -1


class ASI_ERROR_CODE(IntEnum):
    ASI_SUCCESS = 0
    ASI_ERROR_INVALID_INDEX = 1
    ASI_ERROR_INVALID_ID = 2
    ASI_ERROR_INVALID_CONTROL_TYPE = 3
    ASI_ERROR_CAMERA_CLOSED = 4
    ASI_ERROR_CAMERA_REMOVED = 5
    ASI_ERROR_INVALID_PATH = 6
    ASI_ERROR_INVALID_FILEFORMAT = 7
    ASI_ERROR_INVALID_SIZE = 8
    ASI_ERROR_INVALID_IMGTYPE = 9
    ASI_ERROR_OUTOF_BOUNDARY = 10
    ASI_ERROR_TIMEOUT = 11
    ASI_ERROR_INVALID_SEQUENCE = 12
    ASI_ERROR_BUFFER_TOO_SMALL = 13
    ASI_ERROR_VIDEO_MODE_ACTIVE = 14
    ASI_ERROR_EXPOSURE_IN_PROGRESS = 15
    ASI_ERROR_GENERAL_ERROR = 16
    ASI_ERROR_INVALID_MODE = 17
    ASI_ERROR_GPS_NOT_SUPPORTED = 18
    ASI_ERROR_GPS_VER_ERR = 19
    ASI_ERROR_GPS_FPGA_ERR = 20
    ASI_ERROR_GPS_PARAM_OUT_OF_RANGE = 21
    ASI_ERROR_GPS_DATA_INVALID = 22
    ASI_ERROR_END = 23


class ASI_BOOL(IntEnum):
    ASI_FALSE = 0
    ASI_TRUE = 1


class ASI_CONTROL_TYPE(IntEnum):
    ASI_GAIN = 0
    ASI_EXPOSURE = 1
    ASI_GAMMA = 2
    ASI_WB_R = 3
    ASI_WB_B = 4
    ASI_OFFSET = 5
    ASI_BANDWIDTHOVERLOAD = 6
    ASI_OVERCLOCK = 7
    ASI_TEMPERATURE = 8
    ASI_FLIP = 9
    ASI_AUTO_MAX_GAIN = 10
    ASI_AUTO_MAX_EXP = 11
    ASI_AUTO_TARGET_BRIGHTNESS = 12
    ASI_HARDWARE_BIN = 13
    ASI_HIGH_SPEED_MODE = 14
    ASI_COOLER_POWER_PERC = 15
    ASI_TARGET_TEMP = 16
    ASI_COOLER_ON = 17
    ASI_MONO_BIN = 18
    ASI_FAN_ON = 19
    ASI_PATTERN_ADJUST = 20
    ASI_ANTI_DEW_HEATER = 21
    ASI_FAN_ADJUST = 22
    ASI_PWRLED_BRIGNT = 23
    ASI_USBHUB_RESET = 24
    ASI_GPS_SUPPORT = 25
    ASI_GPS_START_LINE = 26
    ASI_GPS_END_LINE = 27
    ASI_ROLLING_INTERVAL = 28


class ASI_EXPOSURE_STATUS(IntEnum):
    ASI_EXP_IDLE = 0
    ASI_EXP_WORKING = 1
    ASI_EXP_SUCCESS = 2
    ASI_EXP_FAILED = 3


# 别名定义
ASI_BRIGHTNESS = ASI_CONTROL_TYPE.ASI_OFFSET
ASI_AUTO_MAX_BRIGHTNESS = ASI_CONTROL_TYPE.ASI_AUTO_TARGET_BRIGHTNESS


# --- 可用于 ctypes 与 DLL 通信的结构体 ---

class ASI_CAMERA_INFO(Structure):
    _fields_ = [
        ("Name", c_char * 64),
        ("CameraID", c_int),
        ("MaxHeight", c_long),
        ("MaxWidth", c_long),
        ("IsColorCam", c_int),
        ("BayerPattern", c_int),
        ("SupportedBins", c_int * 16),
        ("SupportedVideoFormat", c_int * 8),
        ("PixelSize", c_double),
        ("MechanicalShutter", c_int),
        ("ST4Port", c_int),
        ("IsCoolerCam", c_int),
        ("IsUSB3Host", c_int),
        ("IsUSB3Camera", c_int),
        ("ElecPerADU", c_float),
        ("BitDepth", c_int),
        ("IsTriggerCam", c_int),
        ("Unused", c_char * 16),
    ]

class ASI_CONTROL_CAPS(Structure):
    _fields_ = [
        ("Name", c_char * 64),
        ("Description", c_char * 128),
        ("MaxValue", c_long),
        ("MinValue", c_long),
        ("DefaultValue", c_long),
        ("IsAutoSupported", c_int),
        ("IsWritable", c_int),
        ("ControlType", c_int),
        ("Unused", c_char * 32),
    ]

class ASI_ID(Structure):
    _fields_ = [("id", c_char * 8)]

class ASI_SUPPORTED_MODE(Structure):
    _fields_ = [("SupportedCameraMode", c_int * 16)]  # 以 ASI_MODE_END 结尾的数组

class ASI_DATE_TIME(Structure):
    _fields_ = [
        ("Year", c_int),
        ("Month", c_int),
        ("Day", c_int),
        ("Hour", c_int),
        ("Minute", c_int),
        ("Second", c_int),
        ("Msecond", c_int),
        ("Usecond", c_int),
        ("Unused", c_char * 64),
    ]

class ASI_GPS_DATA(Structure):
    _fields_ = [
        ("Datetime", ASI_DATE_TIME),
        ("Latitude", c_double),
        ("Longitude", c_double),
        ("Altitude", c_int),
        ("SatelliteNum", c_int),
        ("Unused", c_char * 64),
    ]



